package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;

import com.google.android.material.tabs.TabLayout;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.OnZTabSelectedListener;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.CommoditylistActivityBinding;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.ui.adapter.OutterPagerAdapter;
import com.sgb.capital.view.ui.fragment.CommodityListFragment;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

/**
 * 作者:张磊
 * 日期:2021/11/17 0028
 * 说明:商品
 */
public class CommodityListActivity extends BaseActivity {
    private CommoditylistActivityBinding mBinding;
    private String mTitles[] = new String[]{"收到的报价", "发起的报价"};
    private List<String> mList = new ArrayList<>();
    private String mTitles2[] = new String[]{"11111111", "222222222222","33333333333333"};
    private int mPosition;

    public static void start(Context context) {
        Intent intent = new Intent(context, CommodityListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.commoditylist_activity);
    }

    public void initData() {
        initTab();
        initTab2();
        List<Fragment>  receiveFragments = new ArrayList<>();
        for (String  s : mTitles2){
            if (s.contains("商品")){
                receiveFragments.add( CommodityListFragment.newInstance("询价"));
            }else if (s.contains("其他")){
                receiveFragments.add( CommodityListFragment.newInstance("询价"));
            }else {
                receiveFragments.add( CommodityListFragment.newInstance("询价"));
            }
        }


        List<Fragment>  initiateFragments = new ArrayList<>();
    }

    private void initTab() {
        for (String title : mTitles) {
            TabLayout.Tab tab = mBinding.tab1.newTab();
            tab.setText(title);
            mBinding.tab1.addTab(tab);
        }
        mBinding.tab1.setTabMode(TabLayout.MODE_FIXED);
        mBinding.tab1.addOnTabSelectedListener(new OnZTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Utils.sendMsg(Constants.EVENT_REFRESH, tab.getPosition());
            }
        });
    }

    private void initTab2() {
        for (String title : mTitles2) {
            TabLayout.Tab tab = mBinding.tab2.newTab();
            tab.setText(title);
            mBinding.tab2.addTab(tab);
            mList.add(title);
        }

        initTabLayout(new OutterPagerAdapter(getSupportFragmentManager() ,mList));
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_REFRESH_COUNT:

                break;
            case Constants
                    .EVENT_CLICK:

                break;
        }
    }


    public void initTabLayout(OutterPagerAdapter adapter) {
        //var adapter = EnquiryPageAdapter(supportFragmentManager, titles)
        mBinding.viewPage.setAdapter(adapter);
        mBinding.tab2.setupWithViewPager(mBinding.viewPage);
        mBinding.viewPage.setCurrentItem(0);
        mBinding.tab2.addOnTabSelectedListener(new OnZTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
            }
        });
    }

    public void initListener() {
        super.initListener();
    }
}